package com.goldfish666.houghrecognition;

import com.goldfish666.houghrecognition.util.Constant;
import com.goldfish666.houghrecognition.util.RecognitionUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

import java.io.File;
import java.util.*;

@Slf4j
@SpringBootApplication
public class HoughRecognitionApplication implements CommandLineRunner {

    public static void main(String[] args) {
        SpringApplication.run(HoughRecognitionApplication.class, args);
    }

    @Override
    public void run(String... args) throws Exception {
        Scanner scanner = new Scanner(System.in);
        log.info("请建立如下文件结构，并按说明保存验证码和基本字符图片：\n" +
                "picture\n" +
                "|---captcha(该文件夹下存放待识别验证码)\n" +
                "    |---captcha1.jpg captcha2.jpg ...\n" +
                "|---source(该文件夹下存放基本字符的图片)\n" +
                "    |---a.jpg b.jpg 0.jpg 3.jpg\n" +
                "|---model(该文件夹用于保存抽象的基本字符模型)\n" +
                "|---result(该文件夹用于保存识别结果)");
        log.info("请输入picture所在目录");
        String input = scanner.nextLine();


        Constant.initialDictionary(input);

        generateDictionary();

        getTargetNameMap();

        getCaptchaNameSet();

        for (String unit : Constant.getTargetSet()) {
            RecognitionUtil.generateShapeBasePicture(unit, Constant.getSourceDictionary() + Constant.getTargetNameMap().get(unit), Constant.getSkeletonDictionary());
        }

        for (String name : Constant.getCaptchaNameSet()) {
            List<RecognitionUtil.Result> result = RecognitionUtil.recognise(Constant.getCaptchaDictionary() + name, Constant.getTargetSet());
            RecognitionUtil.drawResult(result, Constant.getResultDictionary(), name);
            log.info("{} -----success", name);
        }

    }

    private void generateDictionary() {

        File model = new File(Constant.getSkeletonDictionary());
        if (!model.exists()) {
            model.mkdir();
        }

        File result = new File(Constant.getResultDictionary());
        if (!result.exists()) {
            result.mkdir();
        }
    }

    private void getTargetNameMap() {
        Set<String> set = new HashSet<>();
        Map<String, String> map = new LinkedHashMap<>();
        File file = new File(Constant.getSourceDictionary());
        // 获得该文件夹内的所有文件
        File[] sources = file.listFiles();
        assert sources != null;
        for (File f : sources) {
            if (f.isFile()) {
                String s = f.getName();
                String target = s.split("\\.")[0];
                set.add(target);
                map.put(target, s);
            }
        }
        Constant.initialTargetSet(set);
        Constant.initialTargetNameMap(map);
    }

    private void getCaptchaNameSet() {
        Set<String> set = new HashSet<>();
        File file = new File(Constant.getCaptchaDictionary());
        File[] captchaList = file.listFiles();
        assert captchaList != null;
        for (File f : captchaList) {
            if (f.isFile()) {
                set.add(f.getName());
            }
        }
        Constant.initialCaptchaNameSet(set);
    }
}
